package com.siteseat.common.json.prettyprint;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.Status;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

public class PrettyPrintFilter implements ContainerResponseFilter {

  public ContainerResponse filter( ContainerRequest request, ContainerResponse response ) {
    if( ( response.getStatusType().equals( Status.OK ) ) && ( response.getMediaType() != null )
            && ( response.getMediaType().equals( MediaType.APPLICATION_JSON_TYPE ) ) ) {
      if( request.getHeaderValue( "x-pretty-print" ) != null ) {
        response.setContainerResponseWriter( new PrettyResponseAdapter( response.getContainerResponseWriter() ) );
      }
    }

    return response;
  }
}